<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>车辆列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../LayUi/plugin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../LayUi/css/style.css">
    <script type="text/javascript" src="../LayUi/js/jquery.min.js"></script>
    <script type="text/javascript" src="../LayUi/plugin/layui/layui.js"></script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="border-radius: 0;border-top: 4px solid #d2d2d2;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">车牌号码</label>
                    <div class="layui-input-inline">
                        <input type="text" id="carId" name="carId" placeholder="请输入车牌号码" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">车辆类型</label>
                    <div class="layui-input-inline">
                        <select id="carType" name="carType">
                            <option value="">--请选择车辆类型--</option>
                            <option value="大卡车">大卡车</option>
                            <option value="小皮卡">小皮卡</option>
                        </select>
                    </div>
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select id="state" name="state">
                            <option value="">--请选择车辆状态--</option>
                            <option value="正常">正常</option>
                            <option value="维修">维修</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" id="search-btn">查询</button>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" id="reset-btn">重置</button>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin layui-bg-blue" id="add-btn">添加</button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="../../../layui/layui.js"></script>
<script type="text/javascript">

    layui.config({
        base: '../../layui/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['layer','table','form'], function(){
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,table = layui.table;

        // 初始化表格
        var tableIns = table.render({
            elem: '#LAY-user-manage'
            ,url:'/logistics/carmanage/list' //后端接口
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度
            ,title: 'staff'
            ,cols: [[
                {type: 'checkbox', fixed:'left'}
                ,{field:'id', title: 'ID',hide:true}
                ,{field:'carId', title:'车牌号码'}
                ,{field:'carType', title:'车辆类型'}
                ,{field:'payloadWeight', title:'载重',}
                ,{field:'engineModel', title:'发动机型号'}
                ,{field:'state', title:'状态',templet:'#statusTpl'}
                ,{field:'remarks', title:'备注'}
                ,{title: '操作', minWidth: 20, toolbar: '#table-useradmin-webuser', align: "center"}
            ]]
            ,page: {
                curr: 1 //设定初始在第 5 页
                ,limit: 5 //设定初始每页显示 5 条
            },
            limits:[5,10,15,20]
        });

        // 监听查询按钮点击事件
        $('#search-btn').on('click', function() {
            var carId = $('#carId').val(); // 获取输入框的值
            var carType = $('#carType').val();
            var state = $('#state').val();
            table.reload('LAY-user-manage', {
                url: '/logistics/carmanage/findByWhere', // 后端接口地址
                where: {
                    carId: carId, // 发送的查询参数
                    carType: carType,
                    state: state
                }
            });
        });
        // 监听重置按钮点击事件
        $('#reset-btn').on('click', function() {
            table.reload('LAY-user-manage', {
                url: '/logistics/carmanage/list', // 后端接口地址
            });
        });
        // 监听添加按钮点击事件
        $('#add-btn').on('click', function() {
            layer.open({
                type: 2
                ,title: '添加车辆信息'
                ,skin: 'layui-layer-win10'
                ,content: 'add.html'
                ,maxmin: true
                ,area: ['480px', '550px']
                ,btn: ['确定', '取消']
                ,yes: function (index,layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-user-front-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    //监听提交--隐藏按钮
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field; //获取提交的字段
                        // 通过Ajax方式将参数提交到后端--保存数据库中
                        $.ajax({
                            url:"/logistics/carmanage/add",
                            type:"post",
                            data:JSON.stringify(field),
                            contentType: "application/json;charset=utf-8", //提交参数类型为json格式
                            dataType:"json",
                            success:function (res){
                                if(res.code == 0){//成功
                                    layer.msg(res.message);//弹出提示
                                    table.reload('LAY-user-manage'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{//系统出问题
                                    layer.msg(res.message,{icon: 5});//弹出提示
                                }
                            }
                        })
                    });
                    submit.trigger('click');
                }
            });
        });

        //监听行工具事件
        table.on('tool(LAY-user-manage)', function(obj){
            var data = obj.data;
            console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/logistics/carmanage/delete", //请求后端接口地址
                        type:"post", //请求方式get | post
                        data:{id :data.id}, //提交json字符串对象参数
                        dataType:"json", //后端返回参数类型
                        success:function (res){ //请求成功后回调函数res = JSONObject.toJSONString(resultMap)
                            if(res.code == 0){//成功
                                layer.msg(res.msg);//弹出提示
                                table.reload('LAY-user-manage'); //数据刷新
                                layer.close(index); //关闭弹层
                            }
                            if(res.code == -1){//系统出问题
                                console.log(data.ID)
                                layer.msg(res.msg,{icon: 5});//弹出提示
                            }
                        }
                    })
                });
            }else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改车辆'
                    ,skin: 'layui-layer-win10'
                    ,content: 'edit.html'
                    ,maxmin: true
                    ,area: ['480px', '550px']
                    ,btn: ['确定', '取消']
                    ,success: function (layero,index){ //弹出层加载之前执行
                        // 获取选中行的数据
                        // 获取弹出层的表单
                        var body = layer.getChildFrame("body",index);
                        var contentWindow = layero.find('iframe')[0].contentWindow;//得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        // 获取编辑表单中用户名元素
                        // 数据回显到弹出的表单中
                        contentWindow.layui.form.val('layuiadmin-form-useradmin', {
                            "id": data.id
                            ,"carId": data.carId
                            ,"engineModel": data.engineModel
                            ,"payloadWeight": data.payloadWeight
                            ,"roleId": data.roleId
                            ,"status": data.status
                            ,"remarks": data.remarks
                        });
                    }
                    ,yes: function (index,layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交--隐藏按钮
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            // 通过Ajax方式将参数提交到后端--保存数据库中
                            $.ajax({
                                url:"/logistics/carmanage/update",
                                type:"post",
                                data:JSON.stringify(field),
                                contentType: "application/json;charset=utf-8", //提交参数类型为json格式
                                dataType:"json",
                                success:function (res){
                                    if(res.code == 0){//成功
                                        layer.msg(res.message);//弹出提示
                                        table.reload('LAY-user-manage'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    }else{//系统出问题
                                        layer.msg(res.message,{icon: 5});//弹出提示
                                    }
                                }
                            })
                        });
                        submit.trigger('click');
                    }

                });

            }
        });
        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
